package com.tim.app.ui.adapter.found;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.server.entry.Story;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 每日一听的适配器
 */
public class StoryAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Story> {

    static final String TAG = "StoryAdapter";

    private Context mContext;

    private boolean isShowSelf = true;
    private String image;

    private int index = -1;

    public StoryAdapter(Context mContext, List<Story> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    public boolean isShowSelf() {
        return isShowSelf;
    }

    public void setShowSelf(boolean showSelf) {
        isShowSelf = showSelf;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_story, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder mHolder, int position, final Story data) {

        if (null == data)
            return;
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvTitle.setText(data.getTitle());
        } else {
            holder.tvTitle.setText("");
        }
        if (isShowSelf) {
            BitmapLoader.ins().loadImage(data.getImage(), holder.rivImage);
        } else {
            BitmapLoader.ins().loadImage(getImage(), holder.rivImage);
        }
        holder.tvPlaycount.setText(String.valueOf(data.getPvCount()));
        holder.tvTotalTime.setText(data.getTime());
        if (position == getIndex()) {
            holder.tvTitle.setSelected(true);
        } else {
            holder.tvTitle.setSelected(false);
        }
    }


    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivImage;
        TextView tvTitle, tvPlaycount, tvTotalTime;

        public ViewHolder(View itemView) {
            super(itemView);
            rivImage = (RoundedImageView) itemView.findViewById(R.id.rivImage);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvPlaycount = (TextView) itemView.findViewById(R.id.tvPlaycount);
            tvTotalTime = (TextView) itemView.findViewById(R.id.tvTotalTime);
        }

    }

}
